/// @author  Alberto Franco
/// @file    main.go
/// @date    05-05-2011
/// @brief   This is the main file of the TCP simulation 
package netsim

import (
    "fmt"
)

/// Program entry point
func Netsim_Main() {
    var sender   *Sender   = CreateSender("my sender")
    var medium   *Medium   = CreateMedium()
    var receiver *Receiver = CreateReceiver("my receiver")
    /// Some data here to use
    var inputChannel chan int32 = make(chan int32)
    /// Assign channels
    sender.input        = &inputChannel
    sender.ackChan      = &receiver.ackChan
    sender.errorChan    = &medium.errorChan
    
    medium.sendChan     = &sender.pktChan
    
    receiver.transChan  = &medium.transChan
    
    fmt.Println("Starting TCP simulation")
    /// Starts goroutines
    go sender.SendData()
    go medium.Serve()
    go receiver.ReceiveData()
    
    ApplicationLayer(&inputChannel, &receiver.output)
}

/// Simulator of the application layer
func ApplicationLayer(inChan, outChan *chan int32) {
    var pktNum int32 = 0
    var recPkt int32
    for i := 0; i < 10000; i++ {
        fmt.Println("Sending ", pktNum)
        (*inChan) <- pktNum
        recPkt = <- (*outChan)
        fmt.Println("Received", recPkt)
        pktNum += 1
    }
}
